/*
 * This file is part of ddesktop: remote desktop system for iPad 
 *
 * Copyright (C) 2010 Kazuhiro Satoh
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, see <http://www.gnu.org/licenses/>.
 */

#import "SettingControl.h"


@implementation SettingControl

static SettingControl* sharedFactory = nil;  // Singleton object



+ (SettingControl*)sharedManager {  
    @synchronized(self) {  
        if (sharedFactory == nil) {  
            sharedFactory = [[self alloc] init];  
        }  
    }  
    return sharedFactory;  
}  

+ (id)allocWithZone:(NSZone *)zone {  
    @synchronized(self) {  
        if (sharedFactory == nil) {  
            sharedFactory = [super allocWithZone:zone];  
            return sharedFactory;  
        }  
    }  
    return nil;  
}  

- (id)copyWithZone:(NSZone*)zone {  
    return self;
}  

- (id)retain {  
    return self;
}  

- (unsigned)retainCount {  
    return UINT_MAX;
}  

- (void)release {  
}  

- (id)autorelease {  
    return self;
}


+(UIControl*)controlForSettingItem:(SettingItem*)settingItem withFrame:(CGRect)frame
{
	NSObject* value = settingItem.value;
	
	if([value isKindOfClass:[NSString class]]){
		UITextField* textField = [[[UITextField alloc] initWithFrame:frame] autorelease];
		textField.keyboardType = UIKeyboardTypeDefault;
		textField.borderStyle = UITextBorderStyleRoundedRect;
		textField.text = (NSString*)value;
		textField.tag = (int)settingItem;
		[textField addTarget:[SettingControl sharedManager] action:@selector(updateTextFieldValue:) forControlEvents:UIControlEventEditingChanged];
		return textField;
	}
	else if([value isKindOfClass:[NSNumber class]]){
		if([settingItem metaData:@"max"] != nil){
			UISlider* slider = [[[UISlider alloc] initWithFrame:frame] autorelease];
			slider.minimumValue = [(NSNumber*)[settingItem metaData:@"min"] doubleValue];
			slider.maximumValue = [(NSNumber*)[settingItem metaData:@"max"] doubleValue];
			slider.value = [(NSNumber*)value doubleValue];
			slider.tag = (int)settingItem;
			[slider addTarget:[SettingControl sharedManager] action:@selector(updateSliderValue:) forControlEvents:UIControlEventValueChanged];
			return slider;
		}
	}
		
	return nil;
}

			
- (void) updateSliderValue:(UISlider *)aSlider {
	SettingItem* item = (SettingItem*)aSlider.tag;
	item.value = [NSNumber numberWithDouble:aSlider.value];
}

- (void) updateTextFieldValue:(UITextField *)aTextField {
	SettingItem* item = (SettingItem*)aTextField.tag;
	item.value = [NSString stringWithString:aTextField.text];
}


@end
